package com.sfc.spiderweb.service.impl.adb;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csvreader.CsvWriter;
import com.sfc.spiderweb.entity.param.CompareQueryParam;
import com.sfc.spiderweb.entity.po.adb.AmazonDataCompare;
import com.sfc.spiderweb.entity.po.adb.CompareAmazonPlatform;
import com.sfc.spiderweb.entity.po.adb.CompareIstoreOrders;
import com.sfc.spiderweb.entity.po.adb.Orders;
import com.sfc.spiderweb.entity.po.spider.AmazonBillDateSiteColumn;
import com.sfc.spiderweb.eunm.ReshipType;
import com.sfc.spiderweb.mapper.adb.AmazonDataCompareMapper;
import com.sfc.spiderweb.mapper.adb.OrdersMapper;
import com.sfc.spiderweb.service.adb.AmazonDataCompareService;
import com.sfc.spiderweb.service.spider.AmazonBillDateSiteColumnService;
import com.sfc.spiderweb.util.AmountUtils;
import com.sfc.spiderweb.util.BeanUtil;
import com.sfc.spiderweb.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@DS("slave_2")
@Slf4j
public class AmazonDataCompareServiceImpl implements AmazonDataCompareService {

    @Autowired(required = false)
    private AmazonDataCompareMapper amazonDataCompareMapper;

    @Autowired()
    private AmazonBillDateSiteColumnService siteColumnService;

    @Autowired(required = false)
    private OrdersMapper ordersMapper;

    private static List<String> sellingFees = Arrays.asList("selling fees", "手数料", "tarifas de venda", "tarifas de venta", "Verkaufsgebühren", "frais de vente", "Commissioni di vendita");

    private static List<String> productSales = Arrays.asList("product sales", "vendas do produto", "Vendite", "Umsätze", "商品売上", "ventas de productos", "ventes de produits", "verkoop van producten");

    private static List<String> siteList = Arrays.asList("SA", "AE", "SG", "BR", "UK", "COM", "MX", "DE", "IT", "CA", "ES", "JP", "AU", "NL", "FR");

    private static List<String> compareBillDate = Arrays.asList("202001", "202002", "202003", "202004");

    private static List<String> istoreComparePlatfromBillDate = Arrays.asList("201910", "201911", "201912", "202001", "202002", "202003", "202004", "202005", "202006");

    private static String platformCompareIstoreOutPath = "D:\\财务\\账单\\amazon\\amazon_istore\\平台对比istore\\";
    private static String istoreComparePlatformoutPath = "D:\\财务\\账单\\amazon\\amazon_istore\\istore对比平台\\";

    private static String chargebackTimeStartTime = "2020-01-01 00:00:00";

    private static String chargebackTimeEndTime = "2020-05-01 00:00:00";

    private List<String> titleList = Arrays.asList("账号", "币种", "istore订单总金额(原币)", "是否有退款", "istore订单号", "istoreReship订单号", "istore合并订单号", "istore扣费日期",
            "istore订单状态", "平台账单日期", "平台订单号", "订单收入金额", "差异", "平台订单类型", "订单类型");

    @Override
    public void platFromCompareIstore() throws Exception {
        File file = new File(platformCompareIstoreOutPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        FileUtil.deleteAll(platformCompareIstoreOutPath);
        for (String site : siteList) {
            /*if (!site.equals("JP")) {
                continue;
            }*/
            CsvWriter csvWriter = new CsvWriter(platformCompareIstoreOutPath + site + "平台对比istore.csv", ',', Charset.forName("UTF-8"));
            List<List<String>> mainList = new ArrayList<>();
            String ebayCodeColumn = "column4";
            QueryWrapper<AmazonBillDateSiteColumn> wapper = new QueryWrapper<>();
            wapper.lambda().in(AmazonBillDateSiteColumn::getColumnName, productSales);
            wapper.lambda().eq(AmazonBillDateSiteColumn::getSite, site);
            List<AmazonBillDateSiteColumn> list = siteColumnService.list(wapper);
            if (CollectionUtils.isEmpty(list)) {
                log.info("数据异常");
                continue;
            }
            QueryWrapper<AmazonBillDateSiteColumn> columnQueryWrapper = new QueryWrapper<>();
            columnQueryWrapper.lambda().eq(AmazonBillDateSiteColumn::getSite, site);
            columnQueryWrapper.lambda().orderByAsc(AmazonBillDateSiteColumn::getSort);
            List<AmazonBillDateSiteColumn> columnList = siteColumnService.list(columnQueryWrapper);
            if (CollectionUtils.isEmpty(columnList)) {
                log.info("数据异常");
                continue;
            }
            String producSalesColumn = list.get(0).getColumn();
            int flg = 0;
            for (int i = 0; i < columnList.size(); i++) {
                if (columnList.get(i).getColumn().equals(producSalesColumn)) {
                    flg = i;
                }
            }
            List<String> collect = columnList.stream().skip(flg).map(AmazonBillDateSiteColumn::getColumn).collect(Collectors.toList());

            List<String> titleListAdd = new ArrayList<>(titleList);
            for (String s : collect) {
                String columnName = columnList.stream().filter(f -> f.getColumn().equals(s)).findFirst().get().getColumnName();
                titleListAdd.add(columnName);
            }
            mainList.add(titleListAdd);

            CompareQueryParam param = new CompareQueryParam();
            param.setColumn(collect);
            param.setEbayCodeColumn(ebayCodeColumn);
            param.setProducSalesColumn(producSalesColumn);
            param.setSite(site);
            param.setCompareBillDate(compareBillDate);
            param.setType(ReshipType.MAINS.getType());
            //主单
            //List<AmazonDataCompare> amazonList = new ArrayList<>();
            List<AmazonDataCompare> amazonList = amazonDataCompareMapper.getAmazonList(param);
            if (CollectionUtils.isEmpty(amazonList)) {
                continue;
            }
            //退款单
            param.setType(ReshipType.REFUND.getType());
            List<AmazonDataCompare> amazonRefundList = amazonDataCompareMapper.getAmazonList(param);
            List<AmazonDataCompare> dealWithAmazonRefundList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(amazonRefundList)) {
                List<CompareAmazonPlatform> compareAmazonPlatforms = BeanUtil.copyList(amazonRefundList, CompareAmazonPlatform.class).stream().distinct().collect(Collectors.toList());
                List<CompareIstoreOrders> compareIstoreOrders = BeanUtil.copyList(amazonRefundList, CompareIstoreOrders.class).stream().distinct().collect(Collectors.toList());
                dealWithAmazonRefundList = margeRefundCompare(compareAmazonPlatforms, compareIstoreOrders);
            }
            List<AmazonDataCompare> amazonDataCompares = margeOrders(amazonList);
            //分单
            param.setType(ReshipType.BRANCH.getType());
            List<AmazonDataCompare> amazonOtherList = amazonDataCompareMapper.getAmazonOtherList(param);
            List<AmazonDataCompare> amazonBranchList = margeBranchOrders(amazonOtherList);
            //重寄单
            param.setType(ReshipType.RESEND.getType());
            List<AmazonDataCompare> amazonResendList = amazonDataCompareMapper.getAmazonOtherList(param);
            List<List<String>> lsit = excelList(amazonDataCompares, ebayCodeColumn, producSalesColumn, collect);
            List<List<String>> dealWithRefundList = excelList(dealWithAmazonRefundList, ebayCodeColumn, producSalesColumn, collect);
            List<List<String>> branchList = excelList(amazonBranchList, ebayCodeColumn, producSalesColumn, collect);
            List<List<String>> resendList = excelList(amazonResendList, ebayCodeColumn, producSalesColumn, collect);
            if (!CollectionUtils.isEmpty(lsit)) {
                mainList.addAll(lsit);
            }
            if (!CollectionUtils.isEmpty(dealWithRefundList)) {
                mainList.addAll(dealWithRefundList);
            }
            if (!CollectionUtils.isEmpty(branchList)) {
                mainList.addAll(branchList);
            }
            if (!CollectionUtils.isEmpty(resendList)) {
                mainList.addAll(resendList);
            }
            for (List<String> strings : mainList) {
                String[] strings1 = strings.stream().toArray(String[]::new);
                csvWriter.writeRecord(strings1);
            }
            csvWriter.close();
        }
    }

    private List<AmazonDataCompare> margeRefundCompare(List<CompareAmazonPlatform> platformList, List<CompareIstoreOrders> ordersList) {
        List<AmazonDataCompare> dealWithAmazonRefundList = new ArrayList<>();
        if (platformList.size() >= ordersList.size()) {
            Map<String, List<CompareIstoreOrders>> collect1 = ordersList.stream().collect(Collectors.groupingBy(data -> data.getEbayCode()));
            for (CompareAmazonPlatform compareAmazonPlatform : platformList) {
                AmazonDataCompare amazonDataCompare1 = BeanUtil.copyObject(compareAmazonPlatform, AmazonDataCompare.class);
                List<CompareIstoreOrders> compareIstoreOrders1 = collect1.get(compareAmazonPlatform.getEbayCode());
                AmazonDataCompare assemble;
                if (CollectionUtils.isEmpty(compareIstoreOrders1)) {
                    assemble = assemble(amazonDataCompare1, null);
                } else {
                    assemble = assemble(amazonDataCompare1, compareIstoreOrders1.get(0));
                    if (compareIstoreOrders1.size() == 1) {
                        collect1.put(compareAmazonPlatform.getEbayCode(), null);
                    } else {
                        compareIstoreOrders1.remove(0);
                        collect1.put(compareAmazonPlatform.getEbayCode(), compareIstoreOrders1);
                    }
                }
                assemble.setReshipType(ReshipType.REFUND.getType());
                dealWithAmazonRefundList.add(assemble);
            }
        } else {
            Map<String, List<CompareAmazonPlatform>> collect1 = platformList.stream().collect(Collectors.groupingBy(data -> data.getEbayCode()));
            Map<String, List<CompareAmazonPlatform>> findCollect = platformList.stream().collect(Collectors.groupingBy(data -> data.getEbayCode()));
            for (CompareIstoreOrders istoreOrders : ordersList) {
                AmazonDataCompare amazonDataCompare = BeanUtil.copyObject(istoreOrders, AmazonDataCompare.class);
                List<CompareAmazonPlatform> compareAmazonPlatforms1 = collect1.get(istoreOrders.getEbayCode());
                AmazonDataCompare assemble;
                if (CollectionUtils.isEmpty(compareAmazonPlatforms1)) {
                    CompareAmazonPlatform compareAmazonPlatform = new CompareAmazonPlatform();
                    List<CompareAmazonPlatform> compareAmazonPlatforms = findCollect.get(amazonDataCompare.getEbayCode());
                    if (!CollectionUtils.isEmpty(compareAmazonPlatforms)) {
                        compareAmazonPlatform.setEbayCode(compareAmazonPlatforms.get(0).getEbayCode());
                        compareAmazonPlatform.setColumn4(compareAmazonPlatforms.get(0).getColumn4());
                        compareAmazonPlatform.setAccount(compareAmazonPlatforms.get(0).getAccount());
                    }
                    assemble = ordersAssemble(amazonDataCompare, compareAmazonPlatform);
                } else {
                    assemble = ordersAssemble(amazonDataCompare, compareAmazonPlatforms1.get(0));
                    if (compareAmazonPlatforms1.size() == 1) {
                        collect1.put(istoreOrders.getEbayCode(), null);
                    } else {
                        compareAmazonPlatforms1.remove(0);
                        collect1.put(istoreOrders.getEbayCode(), compareAmazonPlatforms1);
                    }
                }
                assemble.setReshipType(ReshipType.REFUND.getType());
                dealWithAmazonRefundList.add(assemble);
            }
        }
        return dealWithAmazonRefundList;
    }

    private AmazonDataCompare assemble(AmazonDataCompare amazonDataCompare, CompareIstoreOrders orders) {
        if (StringUtils.isEmpty(orders)) {
            return amazonDataCompare;
        }
        amazonDataCompare.setCurrency(orders.getCurrency());
        amazonDataCompare.setChargebackTime(orders.getChargebackTime());
        amazonDataCompare.setGrandTotal(orders.getGrandTotal());
        amazonDataCompare.setOrdersCode(orders.getOrdersCode());
        amazonDataCompare.setReshipOrdersCode(orders.getReshipOrdersCode());
        amazonDataCompare.setMargeOrdersCode(orders.getMargeOrdersCode());
        amazonDataCompare.setOrdersStatus(orders.getOrdersStatus());
        amazonDataCompare.setReshipType(orders.getReshipType());
        amazonDataCompare.setSubtotal(orders.getSubtotal());
        amazonDataCompare.setSubOrdersCode(orders.getSubOrdersCode());
        amazonDataCompare.setRefundAmount(orders.getRefundAmount());
        return amazonDataCompare;
    }

    private AmazonDataCompare ordersAssemble(AmazonDataCompare amazonDataCompare, CompareAmazonPlatform amazonPlatform) {
        if (StringUtils.isEmpty(amazonPlatform)) {
            return amazonDataCompare;
        }
        amazonDataCompare.setMonth(amazonPlatform.getMonth());
        amazonDataCompare.setAccount(amazonPlatform.getAccount());
        amazonDataCompare.setProductSales(amazonPlatform.getProductSales());
        amazonDataCompare.setEbayCode(amazonPlatform.getEbayCode());
        amazonDataCompare.setColumn1(amazonPlatform.getColumn1());
        amazonDataCompare.setColumn2(amazonPlatform.getColumn2());
        amazonDataCompare.setColumn3(amazonPlatform.getColumn3());
        amazonDataCompare.setColumn4(amazonPlatform.getColumn4());
        amazonDataCompare.setColumn5(amazonPlatform.getColumn5());
        amazonDataCompare.setColumn6(amazonPlatform.getColumn6());
        amazonDataCompare.setColumn7(amazonPlatform.getColumn7());
        amazonDataCompare.setColumn8(amazonPlatform.getColumn8());
        amazonDataCompare.setColumn9(amazonPlatform.getColumn9());
        amazonDataCompare.setColumn10(amazonPlatform.getColumn10());
        amazonDataCompare.setColumn11(amazonPlatform.getColumn11());
        amazonDataCompare.setColumn12(amazonPlatform.getColumn12());
        amazonDataCompare.setColumn13(amazonPlatform.getColumn13());
        amazonDataCompare.setColumn14(amazonPlatform.getColumn14());
        amazonDataCompare.setColumn15(amazonPlatform.getColumn15());
        amazonDataCompare.setColumn16(amazonPlatform.getColumn16());
        amazonDataCompare.setColumn17(amazonPlatform.getColumn17());
        amazonDataCompare.setColumn18(amazonPlatform.getColumn18());
        amazonDataCompare.setColumn19(amazonPlatform.getColumn19());
        amazonDataCompare.setColumn20(amazonPlatform.getColumn20());
        amazonDataCompare.setColumn21(amazonPlatform.getColumn21());
        amazonDataCompare.setColumn22(amazonPlatform.getColumn22());
        amazonDataCompare.setColumn23(amazonPlatform.getColumn23());
        amazonDataCompare.setColumn24(amazonPlatform.getColumn24());
        amazonDataCompare.setColumn25(amazonPlatform.getColumn25());
        amazonDataCompare.setColumn26(amazonPlatform.getColumn26());
        amazonDataCompare.setColumn27(amazonPlatform.getColumn27());
        amazonDataCompare.setColumn28(amazonPlatform.getColumn28());
        amazonDataCompare.setColumn29(amazonPlatform.getColumn29());
        amazonDataCompare.setColumn30(amazonPlatform.getColumn30());
        return amazonDataCompare;
    }

    private List<AmazonDataCompare> margeOrders(List<AmazonDataCompare> amazonList) {
        Map<String, List<AmazonDataCompare>> collect2 = amazonList.stream().filter(f -> !StringUtils.isEmpty(f.getOrdersCode())).collect(Collectors.groupingBy(data -> data.getOrdersCode()));
        List<AmazonDataCompare> subCollect = amazonList.stream().filter(f -> !StringUtils.isEmpty(f.getSubOrdersCode())).collect(Collectors.toList());
        for (AmazonDataCompare amazonDataCompares : subCollect) {
            String[] split = amazonDataCompares.getSubOrdersCode().split(";");
            BigDecimal bigDecimal = new BigDecimal("0");
            for (String s1 : split) {
                List<AmazonDataCompare> amazonDataCompares1 = collect2.get(s1);
                AmazonDataCompare amazonDataCompare;
                if (CollectionUtils.isEmpty(amazonDataCompares1)) {
                    Orders byOrdersCode = ordersMapper.getByOrdersCode(s1);
                    amazonDataCompare = new AmazonDataCompare();
                    amazonDataCompare.setGrandTotal(byOrdersCode.getSubtotal());
                    amazonDataCompare.setOrdersCode(byOrdersCode.getOrdersCode());
                    amazonDataCompare.setOrdersStatus(byOrdersCode.getOrdersStatus());
                    amazonDataCompare.setCurrency(byOrdersCode.getCode());
                    amazonDataCompare.setEbayCode(byOrdersCode.getEbayCode());
                    amazonDataCompare.setSubtotal(byOrdersCode.getSubtotal());
                    amazonDataCompare.setColumn4(byOrdersCode.getEbayCode());
                } else {
                    amazonDataCompare = amazonDataCompares1.get(0);
                    amazonDataCompare.setGrandTotal(amazonDataCompare.getSubtotal());
                }
                amazonDataCompare.setReshipType(ReshipType.MARGE.getType());
                amazonDataCompare.setChargebackTime(amazonDataCompares.getChargebackTime());
                amazonDataCompare.setMargeOrdersCode(amazonDataCompares.getOrdersCode());
                collect2.put(amazonDataCompare.getOrdersCode(), Arrays.asList(amazonDataCompare));
                bigDecimal = bigDecimal.add(amazonDataCompare.getSubtotal());
            }
            amazonDataCompares.setGrandTotal(amazonDataCompares.getSubtotal().subtract(bigDecimal));
            amazonDataCompares.setMargeOrdersCode(amazonDataCompares.getOrdersCode());
            collect2.put(amazonDataCompares.getOrdersCode(), Arrays.asList(amazonDataCompares));
        }
        List<AmazonDataCompare> amazonDataCompares = new ArrayList<>();
        for (String s : collect2.keySet()) {
            amazonDataCompares.addAll(collect2.get(s));
        }
        return amazonDataCompares;
    }

    private List<AmazonDataCompare> margeBranchOrders(List<AmazonDataCompare> amazonList) {
        Map<String, List<AmazonDataCompare>> collect2 = amazonList.stream().filter(f -> !StringUtils.isEmpty(f.getOrdersCode())).collect(Collectors.groupingBy(data -> data.getReshipOrdersCode()));
        List<AmazonDataCompare> subCollect = amazonList.stream().filter(f -> !StringUtils.isEmpty(f.getSubOrdersCode())).collect(Collectors.toList());
        for (AmazonDataCompare amazonDataCompares : subCollect) {
            String[] split = amazonDataCompares.getSubOrdersCode().split(";");
            BigDecimal bigDecimal = new BigDecimal("0");
            for (String s1 : split) {
                List<AmazonDataCompare> amazonDataCompares1 = collect2.get(s1);
                AmazonDataCompare amazonDataCompare;
                if (CollectionUtils.isEmpty(amazonDataCompares1)) {
                    Orders byOrdersCode = ordersMapper.getByOrdersCode(s1);
                    amazonDataCompare = new AmazonDataCompare();
                    amazonDataCompare.setGrandTotal(byOrdersCode.getSubtotal());
                    amazonDataCompare.setOrdersCode(byOrdersCode.getOrdersCode());
                    amazonDataCompare.setOrdersStatus(byOrdersCode.getOrdersStatus());
                    amazonDataCompare.setCurrency(byOrdersCode.getCode());
                    amazonDataCompare.setEbayCode(byOrdersCode.getEbayCode());
                    amazonDataCompare.setSubtotal(byOrdersCode.getSubtotal());
                } else {
                    amazonDataCompare = amazonDataCompares1.get(0);
                    amazonDataCompare.setGrandTotal(amazonDataCompare.getSubtotal());
                }
                amazonDataCompare.setReshipType(ReshipType.MARGE.getType());
                amazonDataCompare.setChargebackTime(amazonDataCompares.getChargebackTime());
                amazonDataCompare.setMargeOrdersCode(amazonDataCompares.getReshipOrdersCode());
                collect2.put(amazonDataCompare.getReshipOrdersCode(), Arrays.asList(amazonDataCompare));
                bigDecimal = bigDecimal.add(amazonDataCompare.getSubtotal());
            }
            amazonDataCompares.setGrandTotal(amazonDataCompares.getSubtotal().subtract(bigDecimal));
            amazonDataCompares.setMargeOrdersCode(amazonDataCompares.getReshipOrdersCode());
            collect2.put(amazonDataCompares.getReshipOrdersCode(), Arrays.asList(amazonDataCompares));
        }
        List<AmazonDataCompare> amazonDataCompares = new ArrayList<>();
        for (String s : collect2.keySet()) {
            amazonDataCompares.addAll(collect2.get(s));
        }
        return amazonDataCompares;
    }

    /**
     * 组装对象
     *
     * @param amazonList
     * @param ebayCodeColumn
     * @param producSalesColumn
     * @param collect
     * @return
     */
    private List<List<String>> excelList(List<AmazonDataCompare> amazonList, String ebayCodeColumn, String producSalesColumn, List<String> collect) {
        if (CollectionUtils.isEmpty(amazonList)) {
            return null;
        }
        List<List<String>> csvList = new ArrayList<>();
        System.out.println("size=" + amazonList.size() + ",type=" + amazonList.get(0).getReshipType());
        for (AmazonDataCompare amazonDataCompare : amazonList) {
            String productSales = AmountUtils.formatAmount(amazonDataCompare.getProductSales());
            BigDecimal productSalesDecimal = new BigDecimal(productSales);
            String haveRefundOrder;
            if (StringUtils.isEmpty(amazonDataCompare.getRefundAmount())) {
                haveRefundOrder = "无";
            } else {
                haveRefundOrder = "有";
            }
            if (StringUtils.isEmpty(amazonDataCompare.getGrandTotal())) {
                amazonDataCompare.setGrandTotal(new BigDecimal("0"));
            }
            if (amazonDataCompare.getReshipType().equals(ReshipType.REFUND.getType())) {
                amazonDataCompare.setGrandTotal(StringUtils.isEmpty(amazonDataCompare.getRefundAmount()) ? new BigDecimal("0") : amazonDataCompare.getRefundAmount());
            }
            List<String> contentList = new ArrayList<>();
            contentList.add(amazonDataCompare.getAccount());
            contentList.add(amazonDataCompare.getCurrency());
            contentList.add(StringUtils.isEmpty(amazonDataCompare.getGrandTotal()) ? "" : amazonDataCompare.getGrandTotal().toString());
            contentList.add(haveRefundOrder);
            contentList.add(amazonDataCompare.getOrdersCode());
            contentList.add(amazonDataCompare.getReshipOrdersCode());
            contentList.add(amazonDataCompare.getMargeOrdersCode());
            contentList.add(amazonDataCompare.getChargebackTime());
            contentList.add(amazonDataCompare.getOrdersStatus());
            contentList.add(amazonDataCompare.getMonth());
            contentList.add(amazonDataCompare.getEbayCode());
            contentList.add(getColumn(producSalesColumn, amazonDataCompare));
            contentList.add(productSalesDecimal.abs().subtract(amazonDataCompare.getGrandTotal()).toString());
            contentList.add(amazonDataCompare.getColumn3());
            contentList.add(ReshipType.getReshipType(amazonDataCompare.getReshipType()));
            for (String s : collect) {
                contentList.add(getColumn(s, amazonDataCompare));
            }
            csvList.add(contentList);
        }
        return csvList;

    }


    @Override
    public List<String> getColumnList(String site) {
        QueryWrapper<AmazonBillDateSiteColumn> wapper = new QueryWrapper<>();
        wapper.lambda().in(AmazonBillDateSiteColumn::getColumnName, productSales);
        wapper.lambda().eq(AmazonBillDateSiteColumn::getSite, site);
        List<AmazonBillDateSiteColumn> list = siteColumnService.list(wapper);
        if (CollectionUtils.isEmpty(list)) {
            log.info("数据异常");
            return null;
        }
        QueryWrapper<AmazonBillDateSiteColumn> columnQueryWrapper = new QueryWrapper<>();
        columnQueryWrapper.lambda().eq(AmazonBillDateSiteColumn::getSite, site);
        columnQueryWrapper.lambda().orderByAsc(AmazonBillDateSiteColumn::getSort);
        List<AmazonBillDateSiteColumn> columnList = siteColumnService.list(columnQueryWrapper);
        if (CollectionUtils.isEmpty(columnList)) {
            log.info("数据异常");
            return null;
        }
        String producSalesColumn = list.get(0).getColumn();
        int flg = 0;
        for (int i = 0; i < columnList.size(); i++) {
            if (columnList.get(i).getColumn().equals(producSalesColumn)) {
                flg = i;
            }
        }
        List<String> collect = columnList.stream().skip(flg).map(AmazonBillDateSiteColumn::getColumn).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void istoreComparePlatFrom() throws Exception {
        File file = new File(istoreComparePlatformoutPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        FileUtil.deleteAll(istoreComparePlatformoutPath);
        for (String site : siteList) {
            CsvWriter csvWriter = new CsvWriter(istoreComparePlatformoutPath + site + "istore对比平台.csv", ',', Charset.forName("UTF-8"));
            List<List<String>> mainList = new ArrayList<>();
            String ebayCodeColumn = "column4";
            QueryWrapper<AmazonBillDateSiteColumn> wapper = new QueryWrapper<>();
            wapper.lambda().in(AmazonBillDateSiteColumn::getColumnName, productSales);
            wapper.lambda().eq(AmazonBillDateSiteColumn::getSite, site);
            List<AmazonBillDateSiteColumn> list = siteColumnService.list(wapper);
            if (CollectionUtils.isEmpty(list)) {
                log.info("数据异常");
                continue;
            }
            QueryWrapper<AmazonBillDateSiteColumn> columnQueryWrapper = new QueryWrapper<>();
            columnQueryWrapper.lambda().eq(AmazonBillDateSiteColumn::getSite, site);
            columnQueryWrapper.lambda().orderByAsc(AmazonBillDateSiteColumn::getSort);
            List<AmazonBillDateSiteColumn> columnList = siteColumnService.list(columnQueryWrapper);
            if (CollectionUtils.isEmpty(columnList)) {
                log.info("数据异常");
                continue;
            }
            String producSalesColumn = list.get(0).getColumn();
            int flg = 0;
            for (int i = 0; i < columnList.size(); i++) {
                if (columnList.get(i).getColumn().equals(producSalesColumn)) {
                    flg = i;
                }
            }
            List<String> collect = columnList.stream().skip(flg).map(AmazonBillDateSiteColumn::getColumn).collect(Collectors.toList());
            List<String> titleListAdd = new ArrayList<>(titleList);
            for (String s : collect) {
                String columnName = columnList.stream().filter(f -> f.getColumn().equals(s)).findFirst().get().getColumnName();
                titleListAdd.add(columnName);
            }
            mainList.add(titleListAdd);
            CompareQueryParam queryParam = new CompareQueryParam();
            queryParam.setColumn(collect);
            queryParam.setCompareBillDate(istoreComparePlatfromBillDate);
            queryParam.setEbayCodeColumn(ebayCodeColumn);
            queryParam.setProducSalesColumn(producSalesColumn);
            queryParam.setType(0);
            queryParam.setStartTime(chargebackTimeStartTime);
            queryParam.setEndTime(chargebackTimeEndTime);
            queryParam.setSite(site);
            //主单
            List<AmazonDataCompare> amazonList = amazonDataCompareMapper.getIstoreComparePlatformList(queryParam);
            if (CollectionUtils.isEmpty(amazonList)) {
                continue;
            }
            //主单和合并单
            List<AmazonDataCompare> amazonDataCompares = margeOrders(amazonList);
            //退款单
            queryParam.setType(ReshipType.REFUND.getType());
            List<AmazonDataCompare> amazonRefundList = amazonDataCompareMapper.getIstoreComparePlatformList(queryParam);
            List<AmazonDataCompare> dealWithAmazonRefundList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(amazonRefundList)) {
                List<CompareAmazonPlatform> compareAmazonPlatforms = BeanUtil.copyList(amazonRefundList, CompareAmazonPlatform.class).stream().distinct().collect(Collectors.toList());
                List<CompareIstoreOrders> compareIstoreOrders = BeanUtil.copyList(amazonRefundList, CompareIstoreOrders.class).stream().distinct().collect(Collectors.toList());
                dealWithAmazonRefundList = margeRefundCompare(compareAmazonPlatforms, compareIstoreOrders);
            }
            //分单
            queryParam.setType(ReshipType.BRANCH.getType());
            List<AmazonDataCompare> amazonBranchList = amazonDataCompareMapper.getIstoreComparePlatformOtherList(queryParam);
            //重寄单
            queryParam.setType(ReshipType.RESEND.getType());
            List<AmazonDataCompare> amazonResendList = amazonDataCompareMapper.getIstoreComparePlatformOtherList(queryParam);
            List<List<String>> lsit = excelList(amazonDataCompares, ebayCodeColumn, producSalesColumn, collect);
            List<List<String>> refundLsit = excelList(dealWithAmazonRefundList, ebayCodeColumn, producSalesColumn, collect);
            List<List<String>> branchList = excelList(amazonBranchList, ebayCodeColumn, producSalesColumn, collect);
            List<List<String>> resendList = excelList(amazonResendList, ebayCodeColumn, producSalesColumn, collect);
            if (!CollectionUtils.isEmpty(lsit)) {
                mainList.addAll(lsit);
            }
            if (!CollectionUtils.isEmpty(refundLsit)) {
                mainList.addAll(refundLsit);
            }
            if (!CollectionUtils.isEmpty(branchList)) {
                mainList.addAll(branchList);
            }
            if (!CollectionUtils.isEmpty(resendList)) {
                mainList.addAll(resendList);
            }
            for (List<String> strings : mainList) {
                String[] strings1 = strings.stream().toArray(String[]::new);
                csvWriter.writeRecord(strings1);
            }
            csvWriter.close();
        }
    }

    private String getColumn(String column, AmazonDataCompare amazonDataCompare) {
        switch (column) {
            case "column1":
                return amazonDataCompare.getColumn1();
            case "column2":
                return amazonDataCompare.getColumn2();
            case "column3":
                return amazonDataCompare.getColumn3();
            case "column4":
                return amazonDataCompare.getColumn4();
            case "column5":
                return amazonDataCompare.getColumn5();
            case "column6":
                return amazonDataCompare.getColumn6();
            case "column7":
                return amazonDataCompare.getColumn7();
            case "column8":
                return amazonDataCompare.getColumn8();
            case "column9":
                return amazonDataCompare.getColumn9();
            case "column10":
                return amazonDataCompare.getColumn10();
            case "column11":
                return amazonDataCompare.getColumn11();
            case "column12":
                return amazonDataCompare.getColumn12();
            case "column13":
                return amazonDataCompare.getColumn13();
            case "column14":
                return amazonDataCompare.getColumn14();
            case "column15":
                return amazonDataCompare.getColumn15();
            case "column16":
                return amazonDataCompare.getColumn16();
            case "column17":
                return amazonDataCompare.getColumn17();
            case "column18":
                return amazonDataCompare.getColumn18();
            case "column19":
                return amazonDataCompare.getColumn19();
            case "column20":
                return amazonDataCompare.getColumn20();
            case "column21":
                return amazonDataCompare.getColumn21();
            case "column22":
                return amazonDataCompare.getColumn22();
            case "column23":
                return amazonDataCompare.getColumn23();
            case "column24":
                return amazonDataCompare.getColumn24();
            case "column25":
                return amazonDataCompare.getColumn25();
            case "column26":
                return amazonDataCompare.getColumn26();
            case "column27":
                return amazonDataCompare.getColumn27();
            case "column28":
                return amazonDataCompare.getColumn28();
            case "column29":
                return amazonDataCompare.getColumn29();
            case "column30":
                return amazonDataCompare.getColumn30();
        }
        return null;
    }
}
